## Copyright (c) 2005-2006, Gillmer J. Derge.

## This file is part of Civilization IV Warlords ViSa Mod
##
## Civilization IV Warlords ViSa Mod is free software; you can redistribute
## it and/or modify it under the terms of the GNU General Public
## License as published by the Free Software Foundation; either
## version 2 of the License, or (at your option) any later version.
##
## Civilization IV Warlords ViSa Mod is distributed in the hope that it will
## be useful, but WITHOUT ANY WARRANTY; without even the implied
## warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
## See the GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Civilization IV Warlords ViSa Mod; if not, write to the Free
## Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
## 02110-1301 USA

# < edited by the ViSa Modpack Team >
# http://keldath.family-veldman.com
	
import CvConfigParser
import CvEventManager

import CvUnitAllegianceEventManager
import CvUnitStatisticsEventManager
import CvCulturalInfluencesEventManager
import CvCulturalDecayEventManager
import CvEnhancedCultureConquestEventManager
import CvGreatPersonModEventManager
import CvEnhancedTechConquestEventManager
import CvGDEventManager
import CvFieldHospitalEventManager
import CvGreatStatesmanEventManager
import CvJZoCEventManager
import CvViSaMonumentEventManager
import CvViSaIniTweaks
import CvGreatGeneralsFromBarbarianCombatEventManager
import CvViSaTechleakEventManager
import CvPartisanEventManager
import CvRealFortEventManager
import CvGeneticEraEventManager
import CvTechTreeEditorManager
import CvMilitaryBasesEventManager
import CvAIAutoPlay

###############################################################

class CvViSaEventManager(CvEventManager.CvEventManager, object):

	CustomEvents = {}
		
	def __init__(self, *args, **kwargs):           
            		   
		super(CvViSaEventManager, self).__init__(*args, **kwargs)
		# map the initial EventHandlerMap values into the new data structure
		for eventType, eventHandler in self.EventHandlerMap.iteritems():
			self.setEventHandler(eventType, eventHandler)
		
		# --> INSERT EVENT HANDLER INITIALIZATION HERE <--		
                
		CvUnitAllegianceEventManager.CvUnitAllegianceEventManager(self)
		CvUnitStatisticsEventManager.CvUnitStatisticsEventManager(self)
		CvCulturalInfluencesEventManager.CvCulturalInfluencesEventManager(self)
		CvCulturalDecayEventManager.CvCulturalDecayEventManager(self)
		CvEnhancedCultureConquestEventManager.CvEnhancedCultureConquestEventManager(self)
		CvGreatPersonModEventManager.CvGreatPersonModEventManager(self)
		CvEnhancedTechConquestEventManager.CvEnhancedTechConquestEventManager(self)
		CvGDEventManager.CvGDEventManager(self)
		CvFieldHospitalEventManager.CvFieldHospitalEventManager(self)		
		CvGreatStatesmanEventManager.CvGreatStatesmanEventManager(self)
		CvJZoCEventManager.CvJZoCEventManager(self)
		CvViSaMonumentEventManager.ViSaMonumentEventManager(self)
		CvViSaIniTweaks.CvVisaIniTweaks(self)
		CvGreatGeneralsFromBarbarianCombatEventManager.CvGreatGeneralsFromBarbarianCombatEventManager(self)
		CvViSaTechleakEventManager.TechLeakEventManager(self)
		CvPartisanEventManager.CvPartisanEventManager(self)
		CvRealFortEventManager.CvRealFortEventManager(self)
		CvGeneticEraEventManager.CvGeneticEraEventManager(self)
		CvTechTreeEditorManager.CvTechTreeEditorManager(self)
		CvMilitaryBasesEventManager.CvMilitaryBasesEventManager(self)
		
		######################################################
		############### AI AUTOPLAY TWEAK ####################
		#### TEMP ADD
		config = CvConfigParser.CvConfigParser("ViSa-Component-Control.ini")
		
		if( config.getboolean("AIAutoPlay", "AIAutoPlayEnable", True) ) :
			CvAIAutoPlay.AIAutoPlay(self, config)
		
                
############################################################################################################
############################################################################################################
##########################                                                        ##########################
##########################                   for Custom Events                    ##########################
##########################                Thanks to All mod makers                ##########################
##########################                       the ViSa Team                    ##########################
##########################                                                        ##########################
############################################################################################################
############################################################################################################
	def beginEvent( self, context, argsList=-1 ):
		"Begin Event"
		if(self.CustomEvents.has_key(context)):
			return self.CustomEvents[context][2](argsList)
		else:
			return CvEventManager.CvEventManager.beginEvent(self, context, argsList)
			
	def applyEvent( self, argsList ):
		'Apply the effects of an event '
		context, playerID, netUserData, popupReturn = argsList
		
		if(self.CustomEvents.has_key(context)):
			entry = self.CustomEvents[context]
			# the apply function
			return entry[1]( playerID, netUserData, popupReturn )   
		else:
			return CvEventManager.CvEventManager.applyEvent(self, argsList)


	def addCustomEventDefinition(self, eventType, eventDefinition):
		self.CustomEvents[eventType] = eventDefinition

	def removeCustomEventDefinition(self, eventType):
		del self.CustomEvents[eventType]

	def setCustomEventDefinition(self, eventType, eventDefinition):
		self.CustomEvents[eventType] = eventDefinition

	def addEventHandler(self, eventType, eventHandler):
		"""Adds a handler for the given event type.
		
		A list of supported event types can be found in the initialization 
		of EventHandlerMap in the CvEventManager class.

		Throws LookupError if the eventType is not valid.

		"""
#		if eventType not in self.EventHandlerMap:
#			raise LookupError(eventType)
		self.EventHandlerMap[eventType].append(eventHandler)

	def removeEventHandler(self, eventType, eventHandler):
		"""Removes a handler for the given event type.
		
		A list of supported event types can be found in the initialization 
		of EventHandlerMap in the CvEventManager class.  It is an error if 
		the given handler is not found in the list of installed handlers.

		Throws LookupError if the eventType is not valid.

		"""
		if eventType not in self.EventHandlerMap:
			raise LookupError(eventType)
		self.EventHandlerMap[eventType].remove(eventHandler)
	
	def setEventHandler(self, eventType, eventHandler):
		"""Removes all previously installed event handlers for the given 
        event type and installs a new handler.
		
		A list of supported event types can be found in the initialization 
		of EventHandlerMap in the CvEventManager class.  This method is 
		primarily useful for overriding, rather than extending, the default 
		event handler functionality.

		Throws LookupError if the eventType is not valid.

		"""
#		if eventType not in self.EventHandlerMap:
#			raise LookupError(eventType)
		self.EventHandlerMap[eventType] = [eventHandler]

	def handleEvent(self, argsList):
		"""Handles events by calling all installed handlers."""
		self.origArgsList = argsList
		flagsIndex = len(argsList) - 6
		self.bDbg, self.bMultiPlayer, self.bAlt, self.bCtrl, self.bShift, self.bAllowCheats = argsList[flagsIndex:]		
		eventType = argsList[0]
		return {
			"kbdEvent": self._handleConsumableEvent,
			"mouseEvent": self._handleConsumableEvent,
			"OnSave": self._handleOnSaveEvent,
			"OnLoad": self._handleOnLoadEvent
		}.get(eventType, self._handleDefaultEvent)(eventType, argsList[1:])

	def _handleDefaultEvent(self, eventType, argsList):
		if self.EventHandlerMap.has_key(eventType):
			for eventHandler in self.EventHandlerMap[eventType]:
				# the last 6 arguments are for internal use by handleEvent
				eventHandler(argsList[:len(argsList) - 6])

	def _handleConsumableEvent(self, eventType, argsList):
		"""Handles events that can be consumed by the handlers, such as
		keyboard or mouse events.
		
		If a handler returns non-zero, processing is terminated, and no 
		subsequent handlers are invoked.

		"""
		if self.EventHandlerMap.has_key(eventType):
			for eventHandler in self.EventHandlerMap[eventType]:
				# the last 6 arguments are for internal use by handleEvent
				result = eventHandler(argsList[:len(argsList) - 6])
				if (result > 0):
					return result
		return 0

	# TODO: this probably needs to be more complex
	def _handleOnSaveEvent(self, eventType, argsList):
		"""Handles OnSave events by concatenating the results obtained
		from each handler to form an overall consolidated save string.

		"""
		result = ""
		if self.EventHandlerMap.has_key(eventType):
			for eventHandler in self.EventHandlerMap[eventType]:
				# the last 6 arguments are for internal use by handleEvent
				result = result + eventHandler(argsList[:len(argsList) - 6])
		return result

	# TODO: this probably needs to be more complex
	def _handleOnLoadEvent(self, eventType, argsList):
		"""Handles OnLoad events."""
		return self._handleDefaultEvent(eventType, argsList)
